﻿package com.yy.ui.containers {
    import com.yy.ui.core.UIComponent;
    import com.yy.ui.utils.UniformScale;
    
    import flash.display.*;
    import flash.events.*;

    public class TabBarBase extends UIComponent {

        public static const TAB_BAR_CLICK:String = "tab_bar_click";

        private var _labelSelectedBgColor:uint;
        private var _labelDefaultBgColor:uint;
        private var _label:DisplayObject;
        protected var _labelContainer:Sprite;
        private var _labelWidth:uint;
        private var _labelHeigth:uint;
        private var _bgColor:uint;
        private var _bgWidth:uint;
        private var _bgHeight:uint;
        private var _contentDisplayObject:DisplayObject;
        protected var _contentContainer:Sprite;
        private var _labelBg:Shape;
        private var _bg:Shape;
        protected var _labelSelected:Boolean;
        public var selectedIndex:int;

        public function TabBarBase(){
            this._labelDefaultBgColor = 7366730;
            this._labelSelectedBgColor = 14274994;
            this._labelWidth = 100;
            this._labelHeigth = 35;
            this._bgColor = 14274994;
            this._bgWidth = 660;
            this._bgHeight = 420;
            this._labelContainer = new Sprite();
            this._labelContainer.mouseChildren = false;
            this._labelContainer.buttonMode = true;
            this._labelContainer.addEventListener(MouseEvent.CLICK, this.labelOnClick);
            this._contentContainer = new Sprite();
            this.initView();
        }
        private function labelOnClick(_arg1:MouseEvent):void{
            dispatchEvent(new Event(TAB_BAR_CLICK));
        }
        private function initView():void{
            this.initBg();
            this.initLabel();
        }
        private function initLabel():void{
            while (this._labelContainer.numChildren) {
                this._labelContainer.removeChildAt(0);
            };
            if (((this._labelContainer) && (this._labelContainer.parent))){
                this._labelContainer.parent.removeChild(this._labelContainer);
            };
            if (this._labelBg == null){
                this._labelBg = new Shape();
            };
            this._labelBg.graphics.clear();
            if (this._labelSelected){
                this._labelBg.graphics.beginFill(this._labelSelectedBgColor);
            } else {
                this._labelBg.graphics.beginFill(this._labelDefaultBgColor);
            };
            this._labelBg.graphics.drawRoundRect(0, 0, this._labelWidth, this._labelHeigth, 10, 10);
            this._labelBg.graphics.endFill();
            this._labelContainer.addChild(this._labelBg);
            if (this._label){
//                this._label.width = UniformScale.scaleByHeight(this._label.width, this._label.height, this._labelHeigth);
//                this._label.height = this._labelHeigth;
                this._label.x = ((this._labelBg.width - this._label.width) / 2);
                this._label.y = ((this._labelBg.height - this._label.height) / 2);
                this._labelContainer.addChild(this._label);
            };
            addChild(this._labelContainer);
            this.updateUI();
        }
        private function initBg():void{
            while (this._contentContainer.numChildren) {
                this._contentContainer.removeChildAt(0);
            };
            if (((this._contentContainer) && (this._contentContainer.parent))){
                this._contentContainer.parent.removeChild(this._contentContainer);
            };
            if (this._bg == null){
                this._bg = new Shape();
            };
            this._bg.graphics.clear();
            this._bg.graphics.beginFill(this._bgColor);
            this._bg.graphics.drawRoundRect(0, 0, this._bgWidth, this._bgHeight, 10, 10);
            this._bg.graphics.endFill();
            this._contentContainer.addChild(this._bg);
            if (this._contentDisplayObject){
                if (this._contentDisplayObject.width <= this._bgWidth){
                };
                if (this._contentDisplayObject.height <= this._bgHeight){
                };
                this._contentContainer.addChild(this._contentDisplayObject);
            };
            if (this._labelSelected){
                this._contentContainer.visible = true;
            } else {
                this._contentContainer.visible = false;
            };
            addChild(this._contentContainer);
            this.updateUI();
        }
        public function disposeView():void{
        }
        override public function updateUI():void{
            super.updateUI();
            if (this._labelContainer){
                this._labelContainer.y = (-(this._labelContainer.height) + 5);
            };
            this._contentContainer.x = 0;
        }
        public function set labelX(_arg1:uint):void{
            this._labelContainer.x = _arg1;
        }
        public function get labelX():uint{
            return (this._labelContainer.x);
        }
        public function set labelSelectedBgColor(_arg1:uint):void{
            this._labelSelectedBgColor = _arg1;
            this.updateUI();
        }
        public function set labelDefaultBgColor(_arg1:uint):void{
            this._labelDefaultBgColor = _arg1;
            this.updateUI();
        }
        public function set label(_arg1:DisplayObject):void{
            this._label = _arg1;
            this.initLabel();
        }
        public function get label():DisplayObject{
            return ((this._labelContainer as DisplayObject));
        }
        public function get labelWidth():uint{
            return (this._labelWidth);
        }
        public function set labelWidth(_arg1:uint):void{
            this._labelWidth = _arg1;
        }
        public function get labelHeigth():uint{
            return (this._labelHeigth);
        }
        public function set labelHeigth(_arg1:uint):void{
            this._labelHeigth = _arg1;
        }
        public function set bgColor(_arg1:uint):void{
            this._bgColor = _arg1;
        }
        public function get bgWidth():uint{
            return (this._bgWidth);
        }
        public function set bgWidth(_arg1:uint):void{
            this._bgWidth = _arg1;
        }
        public function get bgHeight():uint{
            return (this._bgHeight);
        }
        public function set bgHeight(_arg1:uint):void{
            this._bgHeight = _arg1;
        }
        public function set contentDisplayObject(_arg1:DisplayObject):void{
            this._contentDisplayObject = _arg1;
            this.initBg();
        }
        public function get labelSelected():Boolean{
            return (this._labelSelected);
        }
        public function set labelSelected(_arg1:Boolean):void{
            this._labelSelected = _arg1;
            this.initLabel();
            if (this._labelSelected){
                this._contentContainer.visible = true;
            } else {
                this._contentContainer.visible = false;
            };
        }
        public function set bgVisible(_arg1:Boolean):void{
            if (this._bg){
                this._bg.visible = _arg1;
            };
        }
        public function set labelBgVisible(_arg1:Boolean):void{
            if (this._labelBg){
                this._labelBg.visible = _arg1;
            };
        }

    }
}